/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.gad.ui;

import javax.swing.BorderFactory;
import org.gad.model.CompositeStep;
import org.gad.model.Step;
import org.netbeans.api.visual.anchor.AnchorFactory;
import org.netbeans.api.visual.anchor.AnchorShape;
import org.netbeans.api.visual.router.RouterFactory;
import org.netbeans.api.visual.widget.ConnectionWidget;
import org.netbeans.api.visual.widget.LayerWidget;

/**
 *
 * @author Tomek
 */
public class SplitStepWidget extends CompositeStepWidget{
    private int _childCount = 0;
    private ConnectionWidget _firstConnection;
    
    public SplitStepWidget( BoxScene scene, Step modelStep )
    {
        super( scene, modelStep );
        _clientArea.setBorder(BorderFactory.createEmptyBorder(40,25,40,25));
        
        _clientArea.setLayout(Layouts.HorizontalLayoutSpaced);
        _clientArea.addChild(new DropAreaWidget(scene, false, 
                (CompositeStep)_modelStep, _childCount++));
        
        ConnectionWidget edge = new ConnectionWidget (scene);
        edge.setSourceAnchor (AnchorFactory.createDirectionalAnchor(
                _headerVFlow, AnchorFactory.DirectionalAnchorKind.VERTICAL));
        edge.setTargetAnchor (AnchorFactory.createDirectionalAnchor(
                _bottomWidget, AnchorFactory.DirectionalAnchorKind.VERTICAL));
        edge.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
        //edge.setMaximumSize( new Dimension( 2,2));
        //edge.setRouter (RouterFactory.createOrthogonalSearchRouter( new LayerWidget(scene)) );
        edge.setRouter(RouterFactory.createOrthogonalSearchRouter());
        _firstConnection = edge;
        _scene.getConnectionLayer().addChild(edge);
        
    }

    @Override
    public void addInnerStep(StepWidgetBase step)
    {
        step._parent = this;
        _clientArea.addChild(new InnerStepWrapperWidget(_scene, step, true,
                (CompositeStep)_modelStep, _childCount++));
        
        _firstConnection.setVisible(false);
        
        ConnectionWidget edge1 = new ConnectionWidget (_scene);
        edge1.setSourceAnchor (AnchorFactory.createFreeRectangularAnchor(
                _headerVFlow, true));
        edge1.setTargetAnchor (AnchorFactory.createFreeRectangularAnchor(
                step, true));
        edge1.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
        //edge.setMaximumSize( new Dimension( 2,2));
        edge1.setRouter(SimpleOrthogonalRouter.upperLinksRouter);
        
        ConnectionWidget edge2 = new ConnectionWidget (_scene);
        edge2.setSourceAnchor (AnchorFactory.createFreeRectangularAnchor(
                step, true));
        edge2.setTargetAnchor (AnchorFactory.createFreeRectangularAnchor(
                _bottomWidget, true));
        edge2.setTargetAnchorShape(AnchorShape.TRIANGLE_FILLED);
        //edge.setMaximumSize( new Dimension( 2,2));
        edge2.setRouter(SimpleOrthogonalRouter.lowerLinksRouter);
        
        _scene.getConnectionLayer().addChild(edge1);
        _scene.getConnectionLayer().addChild(edge2);
    }


    @Override
    public void removeInnerStep(StepWidgetBase step)
    {
        (( CompositeStep )_modelStep).RemoveStep(step._modelStep);
    }
    
    

}
